package com.qijia.travel.app;

import java.util.List;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.TouristGroupProfit;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TouristGroupProfitExample;

public class ProfitManager {
	private BaseDAO baseDao;

	public ProfitManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public TouristGroupProfit getTouristGroupProfitById(int touristGroupProfitId)
			throws AppErrorException {
		TouristGroupProfit touristGroupProfit = new TouristGroupProfit();
		touristGroupProfit.setTouristGroupProfitId(touristGroupProfitId);

		try {
			return (TouristGroupProfit) baseDao.selectByPrimaryKey(
					SqlMappedId.TOURIST_GROUP_PROFIT + "."
							+ SqlMappedId.selectByPrimaryKey,
					touristGroupProfit);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取创利信息失败!");
		}
	}

	public void createTouristGroupProfit(TouristGroupProfit touristGroupProfit)
			throws AppErrorException {
		if (touristGroupProfit == null)
			throw new AppErrorException("请输入新增的创利信息");
		try {
			if (touristGroupProfit.getProfitAmount() != null)
				baseDao.insert(SqlMappedId.TOURIST_GROUP_PROFIT + "."
						+ SqlMappedId.insert, touristGroupProfit);
		} catch (TravelDbException e) {
			throw new AppErrorException("新增创利失败,请稍候重试!", e);
		}
	}

	public void updateTouristGroupProfit(TouristGroupProfit touristGroupProfit)
			throws AppErrorException {
		if (touristGroupProfit == null)
			throw new AppErrorException("请选择要进行修改的旅程信息");
		try {
			if (touristGroupProfit.getTouristGroupProfitId() == null) {// 若不存在则新建
				createTouristGroupProfit(touristGroupProfit);
			} else
				baseDao.updateByPrimaryKeySelective(
						SqlMappedId.TOURIST_GROUP_PROFIT + "."
								+ SqlMappedId.updateByPrimaryKeySelective,
						touristGroupProfit);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的旅程["
					+ touristGroupProfit.getTouristGroupDetailId()
					+ "]失败,请稍候重试!", e);
		}
	}

	public void deleteTouristGroupProfit(int touristGroupProfitId)
			throws AppErrorException {
		if (touristGroupProfitId == 0)
			throw new AppErrorException("请选择要删除的旅程创利信息");
		TouristGroupProfit touristGroupProfit = new TouristGroupProfit();
		try {
			touristGroupProfit.setTouristGroupProfitId(touristGroupProfitId);
			baseDao.deleteByPrimaryKey(SqlMappedId.TOURIST_GROUP_PROFIT + "."
					+ SqlMappedId.deleteByPrimaryKey, touristGroupProfit);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的旅程创利信息[编号为:"
					+ touristGroupProfit.getTouristGroupProfitId()
					+ "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TouristGroupProfit> getTouristGroupProfitByExample()
			throws AppErrorException {
		TouristGroupProfitExample example = ExampleFactory
				.createTouristGroupProfitExample();
		example.createCriteria();
		try {
			return (List<TouristGroupProfit>) baseDao.selectByExample(
					SqlMappedId.TOURIST_GROUP_PROFIT + "."
							+ SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的创利信息失败,请稍候重试!", e);
		}
	}

	public PageResult selectTouristGroupProfitList(int curPage)
			throws AppErrorException {
		TouristGroupProfitExample example = ExampleFactory
				.createTouristGroupProfitExample();
		example.createCriteria();
		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.TOURIST_GROUP_PROFIT + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.TOURIST_GROUP_PROFIT + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(10);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取创利数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取创利数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取创利数据列表失败,请稍候重试!", e);
		}
	}
}
